#!/usr/bin/perl
my $colourlightgreen = "00CC00";
my $colourlightred = "FF3300";
my $table1colour = "FFFFFF";
my $table2colour = "66CCFF";
my $command = "pidof -x op_server.pl";
my $command2 = "pidof -x snmpd";
my $command3 = "netstat -anp | grep -i 0.0.0.0:69";
my $command4 = "pidof -x ntpd";
my $command5 = "pidof -x mysqld";
my $command6 = "pidof -x vsftpd";
my $command7 = "pidof -x argusd";
my $command8 = "netstat -an | grep -i 10000";

# Maps a nice printable name to the changing part of the pid file, which
# is also the name of the program
my %servicenames =
(
	'+ asterisk core' => 'asterisk/asterisk',
	'+ web server' => 'httpd',
	'+ cron server' => 'crond',
	'+ sshd server' => 'sshd',
	'+ ddns client' => 'ddclient',
	'+ dhcp server' => 'dhcpd',
);

print "<TABLE>";

my $lines = 0;
my $key = '';


foreach $key (keys %servicenames)
{
        print "<TR>";
        print "<TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>$key</font></TD>";
        my $shortname = $servicenames{$key};
        my $status = &isrunning($shortname);
        print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>$status</font></TD>";
        print "</TR>";
        $lines++;
}
                if (open(FILE, "/etc/cron.daily/00-logwatch"))
                {

					print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ logwatch cron</font></TD>";
					print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

                } else {

					print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ logwatch cron</font></TD>";
					print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
				}
            	    if (system("$command2 &> /dev/null") == 0)
            	    {

						print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ snmpd server</font></TD>";
						print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

    	            } else {

						print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ snmpd server</font></TD>";
						print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
					}

						if (system("$command3 &> /dev/null") == 0)
						{

							print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ tftpd server</font></TD>";
							print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

						} else {

							print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ tftpd server</font></TD>";
							print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
						}

							if (system("$command4 &> /dev/null") == 0)
							{

								print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ ntpd server</font></TD>";
								print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

							} else {

								print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ ntpd server</font></TD>";
								print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
							}

								if (system("$command5 &> /dev/null") == 0)
								{

									print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ mysqld server</font></TD>";
									print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

								} else {

									print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ mysqld server</font></TD>";
									print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
								}

									if (system("$command6 &> /dev/null") == 0)
									{

										print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ vsftpd server</font></TD>";
										print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

									} else {

										print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ vsftpd server</font></TD>";
										print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
									}

										if (system("$command7 &> /dev/null") == 0)
										{

											print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ argusd server</font></TD>";
											print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

										} else {

											print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ argusd server</font></TD>";
											print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
										}

											if (system("$command8 &> /dev/null") == 0)
											{

												print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ webmin server</font></TD>";
												print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

											} else {

													print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ webmin server</font></TD>";
													print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
											}

			if (system("$command &> /dev/null") == 0)

			{

						    print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ flash panel</font></TD>";
						    print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='00CC00'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE></font></TD></TR>";

            		} else {

						    print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ flash panel</font></TD>";
						    print "<TD WIDTH='20%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'><TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='FF3300'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE></font></TD></TR>";
					    }

		print "</TABLE>";

sub isrunning
{
        my $cmd = $_[0];
        my $status = "<TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='$colourlightred'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>fermato</font></TD></TR></TABLE>";
        my $pid = '';
        my $testcmd = '';
        my $exename;

        $cmd =~ /(^[a-z]+)/;
        $exename = $1;

        if (open(FILE, "/var/run/${cmd}.pid"))
        {
                $pid = <FILE>; chop $pid;
                close FILE;
                if (open(FILE, "/proc/${pid}/status"))
                {
                        while (<FILE>)
                        {
                                if (/^Name:\W+(.*)/) {
                                        $testcmd = $1; }
                        }
                        close FILE;
                        if ($testcmd =~ /$exename/)
                        {
                                $status = "<TABLE CELLPADDING='1' CELLSPACING='0' BGCOLOR='$colourlightgreen'><TR><TD><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>attivo</font></TD></TR></TABLE>";
                                if (open(FILE, "/proc/${pid}/cmdline"))
                                {
                                        my $cmdline = <FILE>;
                                        if (!$cmdline) {
                                                $status = "$status (swapped)"; }
                                }
                        }
                }
        }

        return $status;
}

